package com.devplatform.controller;

import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.captcha.generator.MathGenerator;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/api/captcha")
public class CaptchaController {
    @GetMapping
    public void getCaptcha(HttpServletRequest req, HttpServletResponse resp) {
        ShearCaptcha shearCaptcha = new ShearCaptcha(80, 40);
        shearCaptcha.setGenerator(new MathGenerator(1));

        req.getSession().setAttribute("captcha", shearCaptcha.getCode());
        resp.setContentType("image/jpeg");
        resp.setHeader("Cache-Control", "no-cache");
        try {
            shearCaptcha.write(resp.getOutputStream());
            resp.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
